﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyTruongHoc_App
{
    public partial class QLLoaiDiem : Form
    {
        public const String OPT_DIEM_HK = "DiemHK";

        public QLLoaiDiem()
        {
            InitializeComponent();
        }

        private void QLHinhThucCanBo_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void refreshData()
        {
            tbl_LoaiDiemTableAdapter.Connection.ConnectionString = PublicLib.getStrConn();
            tbl_LoaiDiemTableAdapter.ClearBeforeFill = true;
            this.tbl_LoaiDiemTableAdapter.FillByFlag(this.qLTH.tbl_LoaiDiem, PConst.FLAG_NONE);
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            dg_DanhSach.Rows[dg_DanhSach.Rows.Count - 1].Cells[grc_LoaiDiem.Name].Selected = true;
            dg_DanhSach.BeginEdit(true);
        }

        private void dg_DanhSach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value == null)
                return;
            if (dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value.ToString() == "") // add new
            {
                int newId = PublicLib.getMaxIdFromTbl(qLTH.tbl_LoaiDiem.TableName, qLTH.tbl_LoaiDiem.idColumn.ColumnName) + 1;
                dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value = newId;
            }

            //if (dg_DanhSach.Rows[e.RowIndex].Cells[grc_HeSo.Name].Value == null)
            //    dg_DanhSach.Rows[e.RowIndex].Cells[grc_HeSo.Name].Value = 1;

            /*if (e.ColumnIndex == grc_LoaiDiem.Index)
            {
                dg_DanhSach.Rows[e.RowIndex].Cells[grc_HeSo.Name].Selected = true;
                dg_DanhSach.BeginEdit(true);
            }*/

            dg_DanhSach.Rows[e.RowIndex].Cells[grc_Flag.Name].Value = PConst.FLAG_NONE;
        }

        private void fillSTT()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_STT.Name].Value = (i + 1).ToString();
            }
        }

        private void dg_DanhSach_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            fillSTT();
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            dg_DanhSach.SelectedRows[0].Cells[grc_LoaiDiem.Name].Selected = true;
            dg_DanhSach.BeginEdit(true);
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            deleteByID(e.Row.Cells[grc_id.Name].Value.ToString());
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count <= 0)
                return;
            deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
        }

        private void deleteByID(String _id)
        {
            try
            {
                PublicLib.updateField(qLTH.tbl_LoaiDiem.TableName,
                                        qLTH.tbl_LoaiDiem.idColumn.ColumnName,
                                        _id,
                                        qLTH.tbl_LoaiDiem.flagColumn.ColumnName,
                                        PConst.FLAG_DELETE.ToString());
                refreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " " + ex.Message);
            }
        }

        private void tblDanhGiaBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            if ((DataRowView)((BindingSource)sender).Current == null)
                return;
            DataRow ThisDataRow = ((DataRowView)((BindingSource)sender).Current).Row;

            if (ThisDataRow.RowState == DataRowState.Modified ||
                ThisDataRow.RowState == DataRowState.Added)
            {
                tbl_LoaiDiemTableAdapter.Update(ThisDataRow);
            }
        }

        private void QLLoaiDiem_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 15;
            dg_DanhSach.Height = this.Height - pn_Button.Height - 35;

            pn_Button.Left = dg_DanhSach.Right - pn_Button.Width;
            pn_DiemHK.Top = pn_Button.Top = dg_DanhSach.Bottom;
        }

        private void dg_DanhSach_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(MSG.MSG_NHAP_SO);
        }

        private void cb_DiemHK_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_DiemHK.SelectedValue != null)
                PublicLib.setOptionValue(OPT_DIEM_HK, cb_DiemHK.SelectedValue.ToString());
        }

        private void QLLoaiDiem_Shown(object sender, EventArgs e)
        {
            String val = PublicLib.getOptionValue(OPT_DIEM_HK);
            if (val == "")
                cb_DiemHK.SelectedIndex = 0;
            else
            {
                int diemHK = int.Parse(val);
                cb_DiemHK.SelectedValue = diemHK;
            }
        }
    }
}
